package com.yufei.mail.service;

import java.io.IOException;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mchange.v2.log.MLog;
import com.yufei.mail.entity.Email;
import com.yufei.mail.entity.MailSenderInfo;
import com.yufei.utils.CommonUtil;
import com.yufei.utils.ExceptionUtil;

public class MailServiceImpl implements MailService{
	private static Log mLog = LogFactory
			.getLog(MailServiceImpl.class);
	@Override
	public boolean sendEmail(Email email, MailSenderInfo mailSenderInfo) {
		// TODO Auto-generated method stub
		Properties props = System.getProperties(); // 获得系统属性对象
		props.put("mail.smtp.host", mailSenderInfo.getHost()); // 设置SMTP主机
		props.put("mail.smtp.auth", "true");
		System.out.println("准备获取邮件会话对象！");
		try {
			Session mailSession = Session.getInstance(props, null);

		MimeMessage mimeMsg = new MimeMessage(mailSession); // 创建MIME邮件对象
		mimeMsg.setSubject(email.getTitle());
		mimeMsg.setFrom(new InternetAddress(mailSenderInfo.getMailAddress()));
		mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
				.parse(email.getGetEmailAdress()));
		MimeMultipart mp = new MimeMultipart(); // mp 一个multipart对象
		BodyPart bp = new MimeBodyPart();
		bp.setContent(
		email.getContent(),"text/html;charset=GB2312");
		mp.addBodyPart(bp);
		mimeMsg.setContent(mp);
		mimeMsg.saveChanges();
		
	
		//发送邮件
		Transport transport = mailSession.getTransport("smtp"); // ？？？
		
		transport.connect((String) props.get("mail.smtp.host"), mailSenderInfo.getUserName(),
			mailSenderInfo.getPasswd());
		
		transport.sendMessage(mimeMsg, mimeMsg
		.getRecipients(Message.RecipientType.TO));
		// transport.send(mimeMsg);
		System.out.println("发送邮件成功！");
		transport.close();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			MLog.info("发送给"+email.getGetEmailAdress()+"信息失败！错误信息是:"+ExceptionUtil.getExceptionDetailsMessage(e)+"");
			return false;
		}
		return true;
	}
	public static void main(String[] args){
	/*	<property name="host" value="smtp.qq.com" /> 
		<property name="username" value="625070193" /> 
		<property name="password" value="139jasstion" /> */
		for(int i=0;i<100;i++){
			try {
				Thread.currentThread().sleep(1000*5);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		
		MailSenderInfo mailSenderInfo=new MailSenderInfo("smtp.qq.com", "625070193", "139jasstio", "zhao-0244@qq.com");
		try {
			CommonUtil.generateXmlForObject(mailSenderInfo, MailSenderInfo.class,"F:\\jcr\\");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MailService mailService=new MailServiceImpl();
		Email email=new Email(null, "Hello.Test!", "<meta http-equiv=Content-Type content=text/html; charset=gb2312><html>sdfsfsdf"+i+"</html>", "onmygoing@qq.com");
		mailService.sendEmail(email, mailSenderInfo);
		}
	}

}
